package chances.wechat.controller;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import chances.wechat.entity.UserRequest;
import chances.wechat.entity.WechatHistory;
import chances.wechat.entity.WechatStbBind;
import chances.wechat.entity.WechatUser;
import chances.wechat.service.UserRequestService;
import chances.wechat.service.rdb.RdbRequestService;

/**
 * @author shenxw
 *
 * 2017年8月1日
 */
@RestController
@RequestMapping(path = "/fill")
public class FillDataController extends UserAbstractController {
	
	@Autowired
	private RdbRequestService userRequestService;

	@SuppressWarnings("deprecation")
	@RequestMapping("/data/userMasterNo")
	public void bind(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("start execute fill user master stbNo: \n\n");
		
		List<WechatUser> users = this.userRepository.getAllWechatUser();
		for (int i = 0; i < users.size(); i++) {
			WechatUser wechatUser = users.get(i);
			List<WechatStbBind> listBind = this.bindService.listBind(wechatUser.getOpenId());
			if(listBind.size() > 0) {
				WechatStbBind bind = listBind.get(listBind.size() - 1);
				this.userRepository.updateUserMasterStbNo(bind.getStbNo(), wechatUser.getOpenId());
				System.out.println((i + 1)+"  user :"+ URLDecoder.decode(wechatUser.getNickname()) +
						",open id:"+ wechatUser.getOpenId() +
						", master stbno is :" + bind.getStbNo() + 
						", execute success!");
			}
			
		}
		System.out.println("execute over !!!  ");
		
	}
	
	@RequestMapping("/data/historyType")
	public void historyType(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("start execute fill user history content type: \n\n");

		List<WechatHistory> historys = this.userRequestService.getAllUserHistory();
		for (int i = 0; i < historys.size(); i++) {
			WechatHistory history = historys.get(i);
			UserRequest userReqType = this.userRequestService.getUserReqType(history.getContentId(),
					history.getUserId());
			this.userRequestService.updateUserHistory(userReqType.getOpenId(), userReqType.getContentCode(),
					userReqType.getContentType());
			System.out.println((i + 1) + "  open id:" + userReqType.getOpenId() + ", code :"
					+ userReqType.getContentCode() + ", type :" + userReqType.getContentType() 
					+ ", execute success!");
		}

		System.out.println("execute over !!!  ");
	}
	
}
